
$(function() {
    validateRule();
	$('.imgcode').click(function() {
        var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
        $(".imgcode").attr("src", url);
    });
    var flag =true;
    $(document).on("click","#mobilecode",function () {
        console.log("点击了");
        var userName = $("#userName").val();
        console.log(userName);
        console.log(ctx)
        if(flag){
            console.log(ctx)
            $.ajax({
                type: "post",
                url:  ctx+"getLoginSmsCodeWLWX",
                data :{"userName" : userName},
                success: function(data) {
                    flag = false;
                    if(data.code ==="0000"){
                        var time = 120;
                        if(time<=0){
                            flag = true;
                            $("#mobilecode").html("获取验证码");
                            clearInterval(timer);
                        }else{
                            var timer = setInterval(function () {
                                // if(time*1<=0){
                                //     $("#mobilecode").html("获取验证码");
                                //     clearInterval(timer);
                                // }

                                if(time>1){
                                    $("#mobilecode").html(time + "秒");
                                    time--;
                                }else{
                                    flag=true;
                                    $("#mobilecode").html("获取验证码");
                                    clearInterval(timer);
                                }
                            },1000);
                        }
                    }else {
                        flag=true;
                        $.modal.msg(data.msg);
                    }
                }
            })
        }
    })


});


$.validator.setDefaults({
    submitHandler: function() {
		login();
    }
});

function login() {
	$.modal.loading($("#btnSubmit").data("loading"));
	var username = $.common.trim($("input[name='username']").val());
    var password = $.common.trim($("input[name='password']").val());
    var validateCode = $("input[name='validateCode']").val();
    var mobileCode = $("input[name='mobileCode']").val();
    // var rememberMe = $("input[name='rememberme']").is(':checked');
    console.log("点击登录")
    console.log(username)
    console.log(ctx)
    $.ajax({
        type: "post",
        url: ctx + "login",
        data: {
            "username": username,
            "password": password,
            "validateCode" : validateCode,
            "mobileCode" : mobileCode,
            "rememberMe": false

        },
        success: function(r) {
            if (r.code == 0) {
                location.href = ctx + 'index';
            } else {
            	$.modal.closeLoading();
            	$('.imgcode').click();
            	$(".code").val("");
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            },
            // mobileCode: {
            //     required: true
            // }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名",
            },
            password: {
                required: icon + "请输入您的密码",
            },
        //     mobileCode: {
        //         required: icon + "请输入短信验证码",
        //     }
        }
    })
}
